package com.csii.pos.activiti;

import com.csii.adhoc.annotation.OperationLog;
import com.csii.config.esb.EsbSocketReqUtil;
import com.csii.pos.activiti.service.ActivitiScalePlanService;
import com.csii.pos.scaleplan.IfbScalePlan;
import com.csii.result.APIResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: ActivitiScalePlanController
 * @Author: zhanghao
 * @description: 投融资规模计划 的审批流控制层
 * @Date: 2021/7/20 23:04
 * @Version: 1.0
 */
@RestController
@RequestMapping("/actScalePlan")
@Slf4j
public class ActivitiScalePlanController {

    @Autowired
    private ActivitiScalePlanService activitiScalePlanService;

    @Autowired
    private EsbSocketReqUtil esbSocketReqUtil;

    /**
     * 部署流程
     */
    @RequestMapping("/deploy")
    public APIResult<?> deploy() {
        activitiScalePlanService.deploy();
        return APIResult.successResult("部署成功!");
    }

    /**
     * @param ifbScalePlan 操作的业务实体对象,必传主键.
     * @return
     */
    @OperationLog("投融资计划审批提交")
    @RequestMapping("/startAndSubmit")
    public APIResult<?> startAndSubmit(@RequestBody IfbScalePlan ifbScalePlan){
         activitiScalePlanService.startAndSubmit(ifbScalePlan);
         return APIResult.successResult("提交成功!");
    }

    /**
     * 提交资金计划申请
     */
    /*@RequestMapping("/submit")
    public void submit(List<IfbScalePlan> ifbScalePlanList){
        activitiScalePlanService.submit(ifbScalePlanList);
    }*/


    //删除提交审批申请
    /*@RequestMapping("/delete")
    public void delete(List<IfbScalePlan> ifbScalePlanList){
        activitiScalePlanService.delete(ifbScalePlanList);
    }*/


    /**
     * 查询所有进入审批的数据
     * @return
     */
    @OperationLog("投融资计划审批数据查询")
    @RequestMapping("/findAllInfo")
    public APIResult<?> findAllInfo(@RequestBody(required = false) Map<String,String> map){
        List<IfbScalePlan> info = activitiScalePlanService.findAllInfo(map);
        return APIResult.successResult(info);
    }

    /**
     * 审批人员同意
     * @param ifbScalePlan 操作的业务实体对象,必传主键.
     */
    @OperationLog("投融资计划审批同意")
    @RequestMapping("/agree")
    public APIResult<?> agree(@RequestBody IfbScalePlan ifbScalePlan){
        activitiScalePlanService.agreeByApprover(ifbScalePlan);
        //给投融资系统发送通知
//        Map<String, String> param = new HashMap<>();
//        param.put("ifbPlanId", ifbScalePlan.getIfbPlanId());
//        String res = null;
//        try {
//            res = esbSocketReqUtil.sendRequest(param, "tranCode");
//        } catch (Exception e) {
//            log.error("请求esb异常", e);
//            return APIResult.failResult(e, "esb请求异常");
//        }
        return APIResult.successResult("同意操作成功!");
    }


    //
    /**
     * 审批人拒绝
     * @param ifbScalePlan 操作的业务实体对象,必传主键.
     */
    @OperationLog("投融资计划审批拒绝")
    @RequestMapping("/reject")
    public APIResult<?> rejectByApprover(@RequestBody IfbScalePlan ifbScalePlan){
        activitiScalePlanService.rejectByApprover(ifbScalePlan);
        return APIResult.successResult("拒绝操作成功!");
    }

    /**
     * 审批人驳回
     * @param ifbScalePlan 操作的业务实体对象,必传主键.
     */
    @OperationLog("投融资计划审批驳回")
    @RequestMapping("/refute")
    public APIResult<?> refuteByApprover(@RequestBody IfbScalePlan ifbScalePlan){
        activitiScalePlanService.refuteByApprover(ifbScalePlan);
        return APIResult.successResult("驳回操作成功!");
    }

}
